<?php

namespace app\shop\model\user;

use app\common\model\settings\Setting as SettingModel;
use app\common\library\easywechat\AppWx;
use app\common\library\easywechat\AppMp;
use app\common\model\app\App as AppModel;
use app\common\model\app\AppMp as AppMpModel;
use app\common\model\app\AppOpen as AppOpenModel;
use app\common\model\app\AppWx as AppWxModel;
use app\common\service\order\OrderService;
use app\common\library\easywechat\WxPay;
use app\common\model\user\Cash as CashModel;
use app\shop\model\user\User as UserModel;
use app\shop\service\order\ExportService;

/**
 * 余额提现明细模型
 */
class Cash extends CashModel
{
    /**
     * 获取器：申请时间
     */
    public function getAuditTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
    }

    /**
     * 获取器：打款方式
     */
    public function getPayTypeAttr($value)
    {
        return ['text' => $this->payType[$value], 'value' => $value];
    }

    /**
     * 获取提现列表
     */
    public function getList($data)
    {
        $model = $this;
        // 构建查询规则
        $model = $model->alias('cash')
            ->with(['user'])
            ->field('cash.*, user.nickName, user.avatarUrl')
            ->join('user user', 'user.user_id = cash.user_id')
            ->order(['cash.create_time' => 'desc']);
        // 查询条件
        if (isset($data['user_id']) && $data['user_id']) {
            $model = $model->where('cash.user_id', '=', $data['user_id']);
        }
        if (isset($data['search']) && $data['search']) {
            $model = $model->where('user.nickName|user.mobile', 'like', '%' . $data['search'] . '%');
        }
        if (isset($data['apply_status']) && $data['apply_status'] > 0) {
            $model = $model->where('cash.apply_status', '=', $data['apply_status']);
        }
        if (isset($data['pay_type']) && $data['pay_type'] > 0) {
            $model = $model->where('cash.pay_type', '=', $data['pay_type']);
        }
        // 获取列表数据
        $list = $model->paginate($data);
        foreach ($list as &$item) {
            $cancelStatus = 0;
            if ($item['apply_status']['value'] == 50 && $item['pay_time'] + 86400 < time()) {
                $cancelStatus = 1;
            }
            $item['cancelStatus'] = $cancelStatus;
        }
        return $list;
    }

    /**
     * 提现审核
     */
    public function submit($param)
    {
        if ($this['apply_status']['value'] != 10 && $this['apply_status']['value'] != 20) {
            $this->error = '状态错误';
            return false;
        }
        $this->startTrans();
        try {
            $data = ['apply_status' => $param['apply_status']];
            if ($param['apply_status'] == 30) {
                $data['reject_reason'] = $param['reject_reason'];
            }
            // 更新申请记录
            $data['audit_time'] = time();
            self::update($data, ['id' => $param['id']]);
            // 提现驳回：解冻余额
            if ($param['apply_status'] == 30) {
                User::backFreezeMoney($this['user_id'], $this['money']);
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }

    /**
     * 商家转账到零钱
     */
    public function wechatPay()
    {
        if ($this['apply_status']['value'] != 20) {
            $this->error = '状态错误';
            return false;
        }
        // 微信用户信息
        $user = UserModel::detail($this['user_id']);
        // 生成付款订单号
        $orderNO = OrderService::createOrderNo();
        // 付款描述
        $desc = '余额提现付款';
        // 微信支付api：企业付款到零钱
        $open_id = '';
        $app_id = '';
        if ($user['reg_source'] == 'mp') {
            $open_id = $user['mpopen_id'];
            $wxConfig = AppMpModel::getAppMpCache($app_id);
            $app_id = $wxConfig['mpapp_id'];
        } else if ($user['reg_source'] == 'wx') {
            $open_id = $user['open_id'];
            $wxConfig = AppWxModel::getAppWxCache($app_id);
            $app_id = $wxConfig['wxapp_id'];
        } else if ($user['reg_source'] == 'app') {
            $open_id = $user['appopen_id'];
            $wxConfig = AppOpenModel::getAppOpenCache($app_id);
            $app_id = $wxConfig['openapp_id'];
        }

        if ($open_id == '') {
            $this->error = '未找到用户open_id';
            return false;
        }
        $settings = SettingModel::getItem('balance_cash');
        $wxPay = new WxPay(null);
        $pars['real_name'] = $user['real_name'];
        $pars['appid'] = $app_id;
        $pars['desc'] = $desc;
        $pars['real_money'] = $this['real_money'];
        $pars['order_no'] = $orderNO;
        $pars['open_id'] = $open_id;
        $pars['scene_id'] = $settings['scene_id'];
        $resArr = $wxPay->transfer($pars, $user['app_id']);
        if ($resArr['code'] == 0) {
            $this->error = $resArr['message'];
            return false;
        } else {
            if ($resArr['wx_cash_type'] == 1) {
                $this->save([
                    'batch_id' => $resArr['batch_id']
                ]);
                // 确认打款
                $this->money();
                return true;
            } else {
                return $this->save([
                    'out_bill_no' => $resArr['out_bill_no'],
                    'package_info' => $resArr['package_info'],
                    'apply_status' => 50,
                    'pay_time' => time()
                ]);
            }
        }
    }

    /**
     * 提现：微信支付企业付款
     */
    public function wechatPay0()
    {
        // 微信用户信息
        $user = UserModel::detail($this['user_id']);
        // 生成付款订单号
        $orderNO = OrderService::createOrderNo();
        // 付款描述
        $desc = '余额提现付款';
        // 微信支付api：企业付款到零钱
        $open_id = '';
        $app = AppWx::getWxPayApp($user['app_id']);
        if ($user['reg_source'] == 'mp') {
            $open_id = $user['mpopen_id'];
        } else if ($user['reg_source'] == 'wx') {
            $open_id = $user['open_id'];
        } else if ($user['reg_source'] == 'app') {
            $open_id = $user['appopen_id'];
        }

        if ($open_id == '') {
            $this->error = '未找到用户open_id';
            return false;
        }

        $WxPay = new WxPay($app);
        // 请求付款api
        if ($WxPay->transfers($orderNO, $open_id, $this['real_money'], $desc, $user['real_name'])) {
            // 确认已打款
            $this->money();
            return true;
        }
        return false;
    }

    /*
    *统计提现总数量
    */
    public function getUserApplyTotal($apply_status)
    {
        return $this->where('apply_status', '=', $apply_status)->count();
    }

    /**
     * 导出用户余额提现
     */
    public function exportList($data)
    {
        $model = $this;
        // 构建查询规则
        $model = $model->alias('cash')
            ->with(['user'])
            ->field('cash.*, user.nickName, user.avatarUrl,user.mobile')
            ->join('user user', 'user.user_id = cash.user_id')
            ->order(['cash.create_time' => 'desc']);
        // 查询条件
        if (isset($data['user_id']) && $data['user_id']) {
            $model = $model->where('cash.user_id', '=', $data['user_id']);
        }
        if (isset($data['search']) && $data['search']) {
            $model = $model->where('user.nickName|user.mobile', 'like', '%' . $data['search'] . '%');
        }
        if (isset($data['apply_status']) && $data['apply_status'] > 0) {
            $model = $model->where('cash.apply_status', '=', $data['apply_status']);
        }
        if (isset($data['pay_type']) && $data['pay_type'] > 0) {
            $model = $model->where('cash.pay_type', '=', $data['pay_type']);
        }
        // 获取列表数据
        $list = $model->select();
        // 导出excel文件
        (new Exportservice)->userCashList($list);
    }

    public function getPayType()
    {
        $pay_type = [
            ['id' => '10', 'name' => '微信支付'],
            ['id' => '20', 'name' => '支付宝'],
            ['id' => '30', 'name' => '银行卡']
        ];
        return $pay_type;
    }

    /**
     * 撤销付款
     */
    public function cancelPay()
    {
        $this->startTrans();
        try {
            if ($this['apply_status']['value'] != 50) {
                $this->error = '状态错误';
                return false;
            }
            $this->save(['apply_status' => 30, 'reject_reason' => '用户超时未确认收款']);
            User::backFreezeMoney($this['user_id'], $this['money']);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
}